﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : tian_
// Created          : 12-03-2019
//
// Last Modified By : tian_
// Last Modified On : 12-03-2019
// ***********************************************************************
// <copyright file="ModelPropertyDbUtil.cs" company="北京智帆高科科技有限公司">
//     Copyright © 北京智帆高科科技有限公司 2018
// </copyright>
// <summary></summary>
// ***********************************************************************

using System.Collections.Generic;
using System.Linq;
using OSS.BIM.Model;
using ZFGK.Utility;

namespace OSS.BIM.Business.Utility
{
    /// <summary>
    /// Class ModelPropertyDbUtil.
    /// </summary>
    public static class ModelPropertyDbUtil
    {
        /// <summary>
        /// Gets the properties.
        /// </summary>
        /// <param name="modelElement">The model information.</param>
        /// <returns>ModelProperty[].</returns>
        public static ModelProperty[] GetProperties(this ModelElement modelElement)
        {
            return DB.Context.From<ModelProperty>().Where(d => d.ModelElementId == modelElement.ModelElementId).ToEnumerable().ToArray();
        }

        /// <summary>
        /// Updates the specified model properties.
        /// </summary>
        /// <param name="modelProperties">The model properties.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Update(ModelProperty[] modelProperties)
        {
            return DB.Context.Update(modelProperties) > 0;
        }

        /// <summary>
        /// News the identifier if not set.
        /// </summary>
        /// <param name="modelProperty">The model property.</param>
        public static void NewIdIfNotSet(this ModelProperty modelProperty)
        {
            if (modelProperty.ModelPropertyId == 0)
                modelProperty.ModelPropertyId = SnowflakeUtil.GetId();
        }

        /// <summary>
        /// Inserts the specified model properties.
        /// </summary>
        /// <param name="modelProperties">The model properties.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Insert(IEnumerable<ModelProperty> modelProperties)
        {
            // 赋予唯一ID
            modelProperties.ForEach(d => d.NewIdIfNotSet());
            return DB.Context.Insert(modelProperties) > 0;
        }
    }
}
